﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm_Mes.Process
{
    public partial class AddProcessForm : DevExpress.XtraEditors.XtraForm
    {
        public AddProcessForm()
        {
            InitializeComponent();
            Random random = new Random();
            labelControl9.Text = "PROCESS"+random.Next(1000,9999).ToString();
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }

        private void CloseAddProcessSetUpClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddProcessSetUpClick(object sender, EventArgs e)
        {
            // 1. 收集表单数据
            
            var processCode = labelControl9.Text;
            var processName = textEdit3.Text.Trim();
            var isEnabled = radioGroup1?.EditValue; // "1" 或 "0"
           
            var preparationTime = textEdit1.Text.Trim();
            var productionTime = textEdit4.Text.Trim();
            var processDescription = textEdit2.Text.Trim();
            var isQualityCheck = radioGroup6?.EditValue; // "1" 或 "0"
            var remark = memoEdit1.Text.Trim();
            // 2. 构建请求对象
            var newProcess = new
            {
                ProcessCode = processCode,
                ProcessName = processName,
                IsEnabled = isEnabled,
                PreparationTime = preparationTime,
                ProductionTime = productionTime,
                ProcessDescription = processDescription,
                IsQualityCheck = isQualityCheck,
                Remark = remark,
            };

            // 3. 序列化为 JSON
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(newProcess);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            // 4. 发送 POST 请求
            using (var client = new HttpClient())
            {
                ProcessStepUp process = new ProcessStepUp();
                client.BaseAddress = new Uri("http://localhost:5053/"); // 根据实际接口地址调整
                try
                {
                    var response = client.PostAsync("/api/ProcessManagement/AddProcessStepUp", content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        MessageBox.Show("新增成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK; // 关闭弹窗并返回OK
                        
                        this.Close();
                        process.CheckAndPage();
                    }
                    else
                    {
                        string msg = response.Content.ReadAsStringAsync().Result;
                        MessageBox.Show($"新增失败: {msg}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"请求异常: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void AddProcessForm_Load(object sender, EventArgs e)
        {

        }
    }
}